﻿namespace CommandPattern
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 初始化 Receiver、Invoke 和 Command
            Receiver r = new Receiver();
            Command c = new ConcreteCommand(r);
            Invoke i = new Invoke(c);

            // 院领导发出命令
            i.ExecuteCommand();
        }
    }

    // 教官，负责调用命令对象执行请求
    public class Invoke
    {
        public Command _command;

        public Invoke(Command command)
        {
            this._command = command;
        }

        public void ExecuteCommand()
        {
            _command.Action();
        }
    }

    // 命令抽象类
    public abstract class Command
    {
        // 命令应该知道接收者是谁，所以有Receiver这个成语变量
        protected Receiver _receiver;

        public Command(Receiver receiver)
        {
            _receiver = receiver;
        }

        // 命令执行方法
        public abstract void Action();
    }

    public class ConcreteCommand : Command
    {
        public ConcreteCommand(Receiver receiver)
            : base(receiver)
        {

        }

        public override void Action()
        {
            // 调用接收的方法，因为执行命令的是学生
            _receiver.Run100Meters();
        }
    }

    // 命令接收者 -- 学生
    public class Receiver
    {
        public void Run100Meters()
        {
            Console.WriteLine("跑1000米");
        }
    }




    /*
    public class Handler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Command command = CommandFactory.Make(context.Request.Params);
            command.Execute(context);
        }

        public bool IsReusable 
        {
            get 
            {
                return true;
            }
        }
    }

    public interface Command
    {
        void Execute(HttpContext context);
    }

    public class CommandFactory 
    {
        public static Command Make(NameValueCollection parms) 
        {
            string requestParm = parms["requestParm"];
            Command command = null;
            // 根据输入参数得到不同的Command对象
            switch (requestParm) 
            {
                case "1":
                    command = new FirstPortal();
                    break;
                case "2":
                    command = new SecondPortal();
                    break;
                default:
                    command = new FirstPortal();
                    break;
            }
            return command;
        }
    }

    public abstract class RedirectCommand : Command
    {
        private UrlMap map = UrlMap.SoleInstance;
        protected abstract void OnExecute(HttpContext context);
        public void Execute(HttpContext context) 
        {
            OnExecute(context);

            // 根据key和url键值对提交到具体处理的页面
            string url = string.Format("{0}?{1}", map.Map[context.Request.Url.AbsolutePath], context.Request.Url.Query);

            context.Server.Transfer(url);
        }
    }

    public class FirstPortal : RedirectCommand 
    {
        protected override void OnExecute(HttpContext context)
        {
            // 在输入参数中加入项portalId以便页面处理
            context.Items["portalId"] = "1";
        }
    }
    public class SecondPortal : RedirectCommand 
    {
        protected override void OnExecute(HttpContext context)
        {
            context.Items["portalId"] = "2";
        }
    }
    */
}
